//
//  UIViewController+MxSwizzling.m
//  AppShellTool
//
//  Created by IOS01 on 2022/12/8.
//

#import "UIViewController+MxSwizzling.h"
#import <objc/runtime.h>
#import "ShellLuanchTool.h"

@implementation UIViewController (MxSwizzling)

+(void)load{
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        SEL originDisSel = @selector(dismissViewControllerAnimated:completion:);
        SEL overrideDisSel = @selector(sf_dismissViewControllerAnimated:completion:);
        
        Method disMethod = class_getInstanceMethod([self class], originDisSel);
        Method sfDisMethod = class_getInstanceMethod([self class], overrideDisSel);
        
        //原来的类没有实现指定的方法，那么我们就得先做判断，把方法添加进去，然后进行替换
        if (class_addMethod([self class], originDisSel, method_getImplementation(sfDisMethod) , method_getTypeEncoding(disMethod))) {

            class_replaceMethod([self class],
                                overrideDisSel,
                                method_getImplementation(disMethod),
                                method_getTypeEncoding(disMethod));
        }else{
            //交换实现
            method_exchangeImplementations(disMethod, sfDisMethod);
        }
    });
}


- (void)sf_dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion{
    if ([[ShellLuanchTool shareTool] isCanOutApllication]) {
        [[ShellLuanchTool shareTool] shellIsBack];
    }else{
        [self sf_dismissViewControllerAnimated:flag completion:completion];
    }
}

@end
